package com.ruoyi.web.controller.datastatistics;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.basedata.domain.vo.UserTerminalsRequestVO;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.datastatistics.domain.BillBo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.datastatistics.domain.Bill;
import com.ruoyi.datastatistics.service.IBillService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 欠费明细Controller
 *
 * @author zt
 * @date 2025-05-27
 */
@RestController
@RequestMapping("/datastatistics/bill")
public class BillController extends BaseController
{
    @Autowired
    private IBillService billService;

    /**
     * 查询欠费明细列表
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:bill:list')")
    @GetMapping("/list")
    public TableDataInfo list(Bill bill)
    {
        startPage();
        SysUser user = getLoginUser().getUser();
        if(user.getSystemFlag() != Constants.SYS_FLAG_ADMIN){
            bill.setTenantId(user.getTenantId());
        }
//        List<Bill> list = billService.selectBillList(bill);
        List<BillBo> list = billService.selectBillListStat(bill);
        return getDataTable(list);
    }

    /**
     * 导出欠费明细列表
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:bill:export')")
    @Log(title = "欠费明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bill bill)
    {
        List<Bill> list = billService.selectBillList(bill);
        ExcelUtil<Bill> util = new ExcelUtil<Bill>(Bill.class);
        util.exportExcel(response, list, "付款通知单");
    }

    @PreAuthorize("@ss.hasPermi('datastatistics:bill:export')")
    @Log(title = "欠费明细", businessType = BusinessType.EXPORT)
    @PostMapping("/exportStat")
    public void exportStat(HttpServletResponse response, Bill bill)
    {
        List<BillBo> list = billService.selectBillListStat(bill);
        ExcelUtil<BillBo> util = new ExcelUtil<BillBo>(BillBo.class);
        util.exportExcel(response, list, "付款通知单");
    }

    /**
     * 获取欠费明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:bill:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(billService.selectBillById(id));
    }

    /**
     * 新增欠费明细
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:bill:add')")
    @Log(title = "欠费明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bill bill)
    {
        return toAjax(billService.insertBill(bill));
    }

    /**
     * 修改欠费明细
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:bill:edit')")
    @Log(title = "欠费明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bill bill)
    {
        return toAjax(billService.updateBill(bill));
    }

    /**
     * 删除欠费明细
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:bill:remove')")
    @Log(title = "欠费明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(billService.deleteBillByIds(ids));
    }


    @Log(title = "批量结账", businessType = BusinessType.UPDATE)
    @GetMapping("/checkout/{ids}")
    public AjaxResult checkout(@PathVariable Long[] ids)
    {
        billService.updateBillByIds(ids);
        return success();
    }

    /**
     * @Description: 查询月结欠费（未核销）/结算记录（已核销）
     * @date: 2025/6/28 13:53
     * @author: zt
     */
    @GetMapping(value = "/getBillListInGtsf")
    public AjaxResult getBillListInGtsf(String userId,Integer checkFlg){
        return success(billService.getBillList(userId,checkFlg));
    }
    /**
     * @Description: 根据用户id获取该用户下所有设备以及该用户下关联的公摊的其他所有用户的所有设备
     * @date: 2025/10/14
     * @author: kyx
     */
    @GetMapping(value = "/getUserTerminalList")
    public AjaxResult getUserTerminalList(String userId, String departureTime){
        return success(billService.checkBillForUser(userId,departureTime));
    }

    /**
     * @Description: 根据用户id获取该用户下所有设备以及该用户下关联的公摊的其他所有用户的所有设备
     * @date: 2025/10/14
     * @author: kyx
     */
    @GetMapping(value = "/getOtherTerminalList")
    public AjaxResult checkBillForOther(String userId, String departureTime){
        return success(billService.checkBillForOther(userId,departureTime));
    }
    /**
     * @Description: 插入补抄的设备数据
     * @date: 2025/10/15 15:41
     * @author: kyx
     */
    @PostMapping(value = "/dealUserLeaveForinsertHistoryData")
    public AjaxResult dealUserLeaveForinsertHistoryData(@RequestBody UserTerminalsRequestVO requestVO){
        return toAjax(billService.dealUserLeaveForinsertHistoryData(requestVO));
    }
    @GetMapping(value = "/insertBillForUseLeve")
    public AjaxResult insertBillForUseLeve(String userId, String departureTime){
        return success(billService.insertBillForUseLeve(userId,departureTime));
    }
    @GetMapping(value = "/synchronizationWaterData")
    public AjaxResult synchronizationWaterData(String userId, String departureTime){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return success(billService.synchronizationWaterData(null, LocalDateTime.parse(departureTime,formatter)));
    }
    @GetMapping(value = "/synchronizationEletricityData")
    public AjaxResult synchronizationEletricityData(String userId, String departureTime){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return success(billService.synchronizationEletricityData(null, LocalDateTime.parse(departureTime,formatter)));
    }

}
